package com.wx.chapter15.generic_;

import java.util.*;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2023/8/11 - 17:10
 */
public class GenericExercise {
    public static void main(String[] args) {
        HashSet<Student> students = new HashSet<>();
        students.add(new Student("jack",18));
        students.add(new Student("tom", 28));
        students.add(new Student("mary", 19));
        for (Student student : students) {
            System.out.println(student);
        }

        HashMap<String, Student> hm = new HashMap<>();
        hm.put("milan", new Student("milan", 38));
        hm.put("smith", new Student("smith", 48));
        hm.put("hsp", new Student("hsp", 28));
        Set<Map.Entry<String, Student>> entries = hm.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next = iterator.next();
            System.out.println(next.getValue());
        }
    }
}

/*  创建  3 个学生对象
  放入到 HashSet 中学生对象,  使用.
  放入到  HashMap 中，要求 Key  是 String name, Value  就是 学生对象
  使用两种方式遍历 */
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}